﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable disable

namespace Faser_Shop.Models.Database
{
    public partial class Faser_ShopContext : DbContext
    {
        public Faser_ShopContext()
        {
        }

        public Faser_ShopContext(DbContextOptions<Faser_ShopContext> options)
            : base(options)
        {
        }

        public virtual DbSet<LogisticsRecord> LogisticsRecords { get; set; }
        public virtual DbSet<LogisticsWarehouse> LogisticsWarehouses { get; set; }
        public virtual DbSet<Order> Orders { get; set; }
        public virtual DbSet<OrderDetail> OrderDetails { get; set; }
        public virtual DbSet<Product> Products { get; set; }
        public virtual DbSet<ProductCategory> ProductCategories { get; set; }
        public virtual DbSet<ShoppingCart> ShoppingCarts { get; set; }
        public virtual DbSet<User> Users { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=Faser_Shop;Integrated Security=SSPI");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("Relational:Collation", "Chinese_PRC_CI_AS");

            modelBuilder.Entity<LogisticsRecord>(entity =>
            {
                entity.HasKey(e => e.TrackingId)
                    .HasName("PK__Logistic__3C19EDD1631EF466");

                entity.HasOne(d => d.WarehouseCodeNavigation)
                    .WithMany(p => p.LogisticsRecords)
                    .HasForeignKey(d => d.WarehouseCode)
                    .HasConstraintName("FK__Logistics__Wareh__5812160E");
            });

            modelBuilder.Entity<LogisticsWarehouse>(entity =>
            {
                entity.HasKey(e => e.WarehouseId)
                    .HasName("PK__Logistic__2608AFD931320C9D");
            });

            modelBuilder.Entity<Order>(entity =>
            {
                entity.HasOne(d => d.Tracking)
                    .WithMany(p => p.Orders)
                    .HasForeignKey(d => d.TrackingId)
                    .HasConstraintName("FK__Orders__Tracking__5BE2A6F2");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.Orders)
                    .HasForeignKey(d => d.UserId)
                    .HasConstraintName("FK__Orders__UserID__5AEE82B9");
            });

            modelBuilder.Entity<OrderDetail>(entity =>
            {
                entity.HasOne(d => d.Order)
                    .WithMany(p => p.OrderDetails)
                    .HasForeignKey(d => d.OrderId)
                    .HasConstraintName("FK__OrderDeta__Order__5EBF139D");

                entity.HasOne(d => d.Product)
                    .WithMany(p => p.OrderDetails)
                    .HasForeignKey(d => d.ProductId)
                    .HasConstraintName("FK__OrderDeta__Produ__5FB337D6");
            });

            modelBuilder.Entity<Product>(entity =>
            {
                entity.Property(e => e.OnSaleStatus).HasDefaultValueSql("((0))");

                entity.HasOne(d => d.Category)
                    .WithMany(p => p.Products)
                    .HasForeignKey(d => d.CategoryId)
                    .HasConstraintName("FK__Products__Catego__4E88ABD4");
            });

            modelBuilder.Entity<ProductCategory>(entity =>
            {
                entity.HasKey(e => e.CategoryId)
                    .HasName("PK__ProductC__19093A2B7C25307D");
            });

            modelBuilder.Entity<ShoppingCart>(entity =>
            {
                entity.HasKey(e => e.CartId)
                    .HasName("PK__Shopping__51BCD797C7F1BE7F");

                entity.HasOne(d => d.Product)
                    .WithMany(p => p.ShoppingCarts)
                    .HasForeignKey(d => d.ProductId)
                    .HasConstraintName("FK__ShoppingC__Produ__534D60F1");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.ShoppingCarts)
                    .HasForeignKey(d => d.UserId)
                    .HasConstraintName("FK__ShoppingC__UserI__52593CB8");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
